package com.firefoxking.mysite.service.content.impl;

import com.firefoxking.mysite.dao.ContentMapper;
import com.firefoxking.mysite.dao.dto.ContentCond;
import com.firefoxking.mysite.entity.Content;
import com.firefoxking.mysite.service.content.ContentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.hibernate.criterion.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private ContentMapper contentMapper;

    @Override
    public List<Content> getArticlesListByCond(ContentCond contentCond) {
        if (null == contentCond)
            System.out.println("contentCond 为空！");
        List<Content> contents = contentMapper.getArticlesByCond(contentCond);
        return contents;
    }

    @Override
    public PageInfo<Content> getArticlesPagedByCond(ContentCond contentCond, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Content> contents = contentMapper.getArticlesByCond(contentCond);
        return new PageInfo<Content>(contents);
    }

    @Override
    public int addArticle(Content content) {
        return contentMapper.addArticle(content);
    }

    @Override
    public int deleteArticleId(Integer cid) {
        return contentMapper.deleteArticleById(cid);
    }

    @Override
    public int updateArticleById(Content content) {
        return contentMapper.updateArticleById(content);
    }

    @Override
    public Content getArticleById(Integer cid) {
        return contentMapper.getArticleById(cid);
    }

    @Override
    public PageInfo<Content> getRecentlyArticles(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Content> contents = contentMapper.getRecentlyArticle();
        return new PageInfo<Content>(contents);
    }

    @Override
    public PageInfo<Content> searchArticles(String param, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Content> contents = contentMapper.searchArticle(param);
        return new PageInfo<Content>(contents);
    }
}
